package friendsearch;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xiaonei.api.ProfileField;
import com.xiaonei.api.XiaoneiException;
import com.xiaonei.api.XiaoneiRestClient;
import com.xiaonei.api.schema.Friend;
import com.xiaonei.api.schema.FriendsGetFriendsResponse;
import com.xiaonei.api.schema.HometownLocation;
import com.xiaonei.api.schema.User;
import com.xiaonei.api.schema.UsersGetInfoResponse;

public class FriendEarth extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public FriendEarth() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String sessionKey=request.getParameter("xn_sig_session_key");
		XiaoneiRestClient client = new XiaoneiRestClient(Config.apiKey,Config.secret,sessionKey);
		client.setDebug(true);
		int loggedInUserId = 0;
		try {
			loggedInUserId = client.users_getLoggedInUser();
		}
		catch(XiaoneiException e) {
			e.printStackTrace();
		}
		StringBuffer sb=new StringBuffer();
		
		//列出个人信息
		sb.append(buildUserInfo(loggedInUserId));
		//列出好友信息
		try {
			client.friends_getFriends();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		
		sb.append("<div>");
		
		FriendsGetFriendsResponse resp = (FriendsGetFriendsResponse) client.getResponsePOJO();
		List<Friend> list = resp.getFriend();
		List<Integer> fList=new ArrayList();
		
		for (Friend friend : list) {
			//sb.append(buildFriendInfo(client,friend.getId()));
			//sb.append(buildUserInfo(friend.getId()));
			fList.add(friend.getId());
		}
//		for(int id : fList) {
//			System.out.println(id);
//		}
		sb.append(buildFriendInfo(client,fList));
		sb.append("</div>");
//		System.out.print(sb.toString());
		out.println(sb.toString());
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	private String buildUserInfo(int id) {
		StringBuffer sb=new StringBuffer();
		sb.append("<div align=\"center\">\n");
		sb.append("图片：<xn:profile-pic uid=\"");
		sb.append(id);
		sb.append("\" linked=\"true\" size=\"tiny\"/><br>\n");
		sb.append("姓名：<xn:name uid=\""+id+"\" linked=\"true\" shownetwork=\"true\" />\n");
		sb.append("</div>\n");
		return sb.toString();
	}
	
	private String buildFriendInfo(XiaoneiRestClient client,List<Integer> flist) {
		StringBuffer sb=new StringBuffer();
		
		EnumSet<ProfileField> enumFields = EnumSet.of(ProfileField.NAME,
				ProfileField.HOMETOWN_LOCATION);
		try {
			client.users_getInfo(flist, enumFields);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		UsersGetInfoResponse loggedUserGetInfoRes = (UsersGetInfoResponse) client.getResponsePOJO();
		List<User> users = loggedUserGetInfoRes.getUser();
		HometownLocation hl=null;
		for (User user : users) {
			sb.append("<div align=\"center\">\n");
			sb.append("图片：<xn:profile-pic uid=\"");
			sb.append(user.getUid());
			sb.append("\" linked=\"true\" size=\"tiny\"/><br>\n");
			sb.append("姓名：<xn:name uid=\""+user.getUid()+"\" linked=\"true\" shownetwork=\"true\" />\n");
			hl = user.getHometownLocation();
			if(hl!=null) {
				sb.append("家乡:"+hl.getProvince()+"省"+hl.getCity());
			}
			sb.append("</div>\n");
			
		}
		//System.out.println("InFriendInfo:\n"+sb.toString());
		return sb.toString();
	}
}
